package com.tansuo365.test1.controller.excelcontrol;

import com.alibaba.excel.EasyExcel;
import com.tansuo365.test1.bean.InfoData;
import com.tansuo365.test1.bean.kucunbean.ChukuDetails;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.bean.otherbean.DWuliao;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.bean.otherbean.ShengouZhixing;
import com.tansuo365.test1.service.excelservice.ShengouDetailsExcelService;
import com.tansuo365.test1.service.excelservice.WuliaoExcelService;
import com.tansuo365.test1.service.kucunservice.ChukuDetailsService;
import com.tansuo365.test1.service.kucunservice.WuliaoKucunRiZhiService;
import com.tansuo365.test1.service.kucunservice.WuliaoKucunSearService;
import com.tansuo365.test1.service.otherservice.DWuliaoService;
import com.tansuo365.test1.service.otherservice.ShengouDetailsService;
import com.tansuo365.test1.service.otherservice.ShengouZhixingService;
import com.tansuo365.test1.service.otherservice.ShengouZongService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

//import com.institutecloud.msfileservice.bean.goods_price.Pcoke;

//excel导入控制层
@RequestMapping("/excel")
@RestController
public class ExcelExportController {

    @Autowired //1.物料表导入导出
    private WuliaoExcelService wuliaoExcelService;
    @Autowired //2.申购单导出
    private ShengouDetailsExcelService shengouDetailsExcelService;
//    @Autowired
//    private ShengouDetailsService shengouDetailsService;
//    @Autowired //3.申购执行导出
//    private ShengouZhixingService shengouZhixingService;
//    @Autowired //4.库存查询导出
//    private WuliaoKucunSearService wuliaoKucunSearService;
//    @Autowired
//    private WuliaoKucunRiZhiService wuliaoKucunRiZhiService;
//    @Autowired //5.用量导出
//    private ChukuDetailsService chukuDetailsService;

    private Class clazzz;

    public void setClazz(Class clazz) {
        clazzz = clazz;
    }

    public Class getClazz() {
        return clazzz;
    }

    @PostMapping("/exportExcel/{type}")
    public int exportExcel(HttpServletResponse response, @RequestParam(value = "downloadExcelName") String downloadExcelName,
                           @RequestParam(value = "ids[]") Integer[] ids, @PathVariable String type) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File desktopPath = fsv.getHomeDirectory(); //获取桌面路径
        //开始获取数据
        List<InfoData> returnList = null;
        switch (type) {
            case "wuliao":
                setClazz(DWuliao.class);
                returnList = wuliaoExcelService.selectInfoDataByPKArr(ids);
                break;
//            case "shengoudan" : setClazz(ShengouDetails.class); returnList = shengouDetailsService.selectInfoDataByPKArr(ids);break;
//            case "shengouzhixing" : setClazz(ShengouZhixing.class); returnList = shengouZhixingService.selectInfoDataByPKArr(ids);break;
//            case "kucunchaxun" : setClazz(WuliaoKucunRiZhi.class); returnList = wuliaoKucunSearService.selectInfoDataByPKArr(ids);break;
//            case "yongliangchaxun" : setClazz(ChukuDetails.class); returnList = chukuDetailsService.selectInfoDataByPKArr(ids);break;
            default:
                break;
        }
//        可选字段
        Set<String> excludeColumnFiledNames = new HashSet<>();
        excludeColumnFiledNames.add("id");
        excludeColumnFiledNames.add("p_id");
        excludeColumnFiledNames.add("w_id");
        excludeColumnFiledNames.add("o_id");
        excludeColumnFiledNames.add("state");
        excludeColumnFiledNames.add("sort");
        excludeColumnFiledNames.add("order");
        excludeColumnFiledNames.add("createtime");
        excludeColumnFiledNames.add("updatetime");
        excludeColumnFiledNames.add("cid");
        excludeColumnFiledNames.add("uid");
        excludeColumnFiledNames.add("delstate");
        try {
//            String fileName = URLEncoder.encode(downloadExcelName, "UTF-8");
            String fileName = downloadExcelName;
            OutputStream out = new FileOutputStream(desktopPath + "/" + fileName +"-"+ System.currentTimeMillis()+".xlsx");

//            EasyExcel.write(out,getClazz()).autoCloseStream(Boolean.FALSE).sheet(type).doWrite(returnList);

//            可选字段
            EasyExcel.write(out, getClazz()).excludeColumnFiledNames(excludeColumnFiledNames).
                    autoCloseStream(Boolean.FALSE).sheet(type).doWrite(returnList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
        return 1;
    }

    /**
     * 根据选中物料类别进行导出
     * @param response
     * @param downloadExcelName
     * @param type
     * @param w_id 大级别分类
     * @param o_id 子级别分类
     * @return
     * @throws IOException
     */
    @PostMapping("/exportTypeExcel/{type}")
    public int exportTypeExcel(HttpServletResponse response, @RequestParam(value = "downloadExcelName") String downloadExcelName,
                            @PathVariable String type,@RequestParam("w_id")Integer w_id,@RequestParam("o_id")Integer o_id) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File desktopPath = fsv.getHomeDirectory(); //获取桌面路径
        //开始获取数据
        List<InfoData> returnList = null;
        switch (type) {
            case "wuliaoType":
                setClazz(DWuliao.class);
                returnList = wuliaoExcelService.selectInfoDataByTypeId(w_id,o_id);
                break;
//            case "shengoudan" : setClazz(ShengouDetails.class); returnList = shengouDetailsService.selectInfoDataByPKArr(ids);break;
//            case "shengouzhixing" : setClazz(ShengouZhixing.class); returnList = shengouZhixingService.selectInfoDataByPKArr(ids);break;
//            case "kucunchaxun" : setClazz(WuliaoKucunRiZhi.class); returnList = wuliaoKucunSearService.selectInfoDataByPKArr(ids);break;
//            case "yongliangchaxun" : setClazz(ChukuDetails.class); returnList = chukuDetailsService.selectInfoDataByPKArr(ids);break;
            default:
                break;
        }
//        可选字段
        Set<String> excludeColumnFiledNames = new HashSet<>();
        excludeColumnFiledNames.add("id");
        excludeColumnFiledNames.add("p_id");
        excludeColumnFiledNames.add("w_id");
        excludeColumnFiledNames.add("o_id");
        excludeColumnFiledNames.add("state");
        excludeColumnFiledNames.add("sort");
        excludeColumnFiledNames.add("order");
        excludeColumnFiledNames.add("createtime");
        excludeColumnFiledNames.add("updatetime");
        excludeColumnFiledNames.add("cid");
        excludeColumnFiledNames.add("uid");
        excludeColumnFiledNames.add("delstate");
        try {
//            String fileName = URLEncoder.encode(downloadExcelName, "UTF-8");
            String fileName = downloadExcelName;
            OutputStream out = new FileOutputStream(desktopPath + "/" + fileName +"-"+ System.currentTimeMillis()+".xlsx");

//            可选字段
            EasyExcel.write(out, getClazz()).excludeColumnFiledNames(excludeColumnFiledNames).
                    autoCloseStream(Boolean.FALSE).sheet(type).doWrite(returnList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
        return 1;
    }

    /**
     *
     * @param response
     * @param downloadExcelName
     * @param zongzhangid
     * @param type
     * @return
     * @throws IOException
     */
    @PostMapping("/exportShengouDetailsExcel/{type}")
    public int exportShengouDetailsExcel(HttpServletResponse response, @RequestParam(value = "downloadExcelName") String downloadExcelName,
                           @RequestParam(value = "zongzhangid") Integer zongzhangid, @PathVariable String type) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File desktopPath = fsv.getHomeDirectory(); //获取桌面路径
        //开始获取数据
        List<InfoData> returnList = null;
        switch (type) {
            case "shengouxx":
                setClazz(ShengouDetails.class);
                returnList = shengouDetailsExcelService.selectInfoDataByPid(zongzhangid);
                break;
//            case "shengoudan" : setClazz(ShengouDetails.class); returnList = shengouDetailsService.selectInfoDataByPKArr(ids);break;
//            case "shengouzhixing" : setClazz(ShengouZhixing.class); returnList = shengouZhixingService.selectInfoDataByPKArr(ids);break;
//            case "kucunchaxun" : setClazz(WuliaoKucunRiZhi.class); returnList = wuliaoKucunSearService.selectInfoDataByPKArr(ids);break;
//            case "yongliangchaxun" : setClazz(ChukuDetails.class); returnList = chukuDetailsService.selectInfoDataByPKArr(ids);break;
            default:
                break;
        }
//        可选字段
        Set<String> excludeColumnFiledNames = new HashSet<>();
        excludeColumnFiledNames.add("id");
        excludeColumnFiledNames.add("zhuanye");
        excludeColumnFiledNames.add("sort");
        excludeColumnFiledNames.add("order");
        excludeColumnFiledNames.add("createtime");
        excludeColumnFiledNames.add("updatetime");
        excludeColumnFiledNames.add("zongzhangid");
        try {
//            String fileName = URLEncoder.encode(downloadExcelName, "UTF-8");
            String fileName = downloadExcelName;
            OutputStream out = new FileOutputStream(desktopPath + "/" + fileName +"-"+ System.currentTimeMillis()+".xlsx");

//            EasyExcel.write(out,getClazz()).autoCloseStream(Boolean.FALSE).sheet(type).doWrite(returnList);

//            可选字段
            EasyExcel.write(out, getClazz()).excludeColumnFiledNames(excludeColumnFiledNames).
                    autoCloseStream(Boolean.FALSE).sheet(type).doWrite(returnList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
        return 1;
    }
///**
}
